﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text;

public partial class SpecialMediator : System.Web.UI.Page
{
    public string Classn;
    public string toptitle, topContent, addtime, smallClassName, bigClassName, liHtml;
    string bigClassID = string.Empty;
    string smallClassID = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["bigclassid"] != null) { bigClassID = Request.QueryString["bigclassid"].ToString(); }
        if (Request.QueryString["smallclassid"] != null) { smallClassID = Request.QueryString["smallclassid"].ToString(); }
        if (IsNumeric(bigClassID) == false) { bigClassID = "8"; }
        if (IsNumeric(smallClassID) == false) { smallClassID = "36"; }

        DataSet dslink = Socut.Data.ExecuteDataSet(string.Format("SELECT id,smallclassname FROM dbo.web_notice_smallclass WHERE bigclassid={0} ORDER BY smallclassorder", bigClassID));
        StringBuilder liBuilder = new StringBuilder();
        foreach (DataRow row in dslink.Tables[0].Rows)
        {
            if (row["id"].ToString() == smallClassID)
            {
                liBuilder.Append(string.Format("<li><a href=\"SpecialMediator.aspx?bigclassid={0}&smallclassid={1}\" class=\"content-left-current\">{2}</a></li>", bigClassID, row["id"].ToString(), row["smallclassname"].ToString()));    
            }
            else
            {
                liBuilder.Append(string.Format("<li><a href=\"SpecialMediator.aspx?bigclassid={0}&smallclassid={1}\">{2}</a></li>", bigClassID, row["id"].ToString(), row["smallclassname"].ToString()));
            }
        }
        liHtml = liBuilder.ToString();

        if (bigClassID != "" && smallClassID != "")
            {
                bigClassName = ShowData("bigclassname", string.Format("SELECT bigclassname FROM dbo.web_notice_bigclass WHERE id={0}", bigClassID));
                smallClassName = ShowData("smallclassname", string.Format("SELECT smallclassname FROM dbo.web_notice_smallclass WHERE bigclassid={0} AND id={1}", bigClassID, smallClassID));
                topContent = ShowContentData();
            }

    }

    private string ShowData(string fieldName,string sql)
    {
        string result = string.Empty;
        Socut.Reader sqlDataReader = new Socut.Reader(sql);
        try
        {
            sqlDataReader.Read();
            result = htmlstr(sqlDataReader[fieldName].ToString());
        }
        catch
        {
            Response.Write("为保证网站的服务质量，服务器临时维护中...请稍候!");
            Response.End();
        }        

        sqlDataReader.Close();
        return result;
    }

    private string ShowContentData()
    {
        string sql = string.Format("SELECT picurl,content FROM dbo.web_notice WHERE bigclassid={0} AND smallclassid={1}", bigClassID, smallClassID);
        Socut.Reader sqlDataReader = new Socut.Reader(sql);
        try
        {
            StringBuilder sb = new StringBuilder();
            while (sqlDataReader.Read())
            {
                sb.Append("<table width=\"100%\" border=\"0\"><tr>");
                string img = string.Format("<img src=\"newsimage/{0}\" alt=\"\" width=\"148\" height=\"198\" hspace=\"12\" align=\"left\" />", sqlDataReader["picurl"].ToString());
                sb.Append(string.Format("<td width=\"20%\">{0}</td>", img));
                sb.Append(string.Format("<td width=\"80%\" align=\"left\" valign=\"top\">{0}</td>", htmlstr(sqlDataReader["content"].ToString())));
                sb.Append("</tr></table>");
            }
            sqlDataReader.Close();
            return sb.ToString();
        }
        catch
        {
            return "为保证网站的服务质量，服务器临时维护中...请稍候!";
        }
    }

    public string htmlstr(string chr)
    {
        if (chr == null)
            return "";
        chr = chr.Replace("\n", "<br>");
        chr = chr.Replace("select", "");
        chr = chr.Replace("delete", "");
        chr = chr.Replace("update", "");
        chr = chr.Replace("drop", "");
        chr = chr.Replace("and", "");
        chr = chr.Replace("or", "");
        return (chr);
    }

    static bool IsNumeric(string str)
    {
        if (str == null || str.Length == 0)
            return false;
        foreach (char c in str)
        {
            if (!Char.IsNumber(c))
            {
                return false;
            }
        }
        return true;
    }
}
